﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SphinxSearchApi;

namespace SphinxTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //var result = sphinxClient.Query("'^san*')");

            //using (var sphinxClient = new SphinxClientPersistent("192.168.1.210", 3312))
            //{
            //    var status = sphinxClient.Status();
            //}

            //SearchFacilityNearby();
            //SearchGeo();

            SearchTest();
        }

        static void SearchGeo()
        {
            string cityName = "phoenix";
            string stateName = "AZ";
            string countryName = "US";

            var sphinxClient = new SphinxClient("192.168.1.210", 9312);
            sphinxClient.Limit = 5;
            sphinxClient.MatchMode = MatchEnum.Extended2;
            sphinxClient.Sort = SortEnum.Expression;
            sphinxClient.SortBy = string.Format(
                "@weight + IF(({0} / namelength) * 100=100, population, ({0} / namelength) * 100)",
                cityName.Length.ToString()
                );

            sphinxClient.FieldWeights.Add("Name", 10);

            //foreach (RankEnum item in Enum.GetValues(typeof(RankEnum)))
            //{
            //    sphinxClient.Ranker = item;
            sphinxClient.AddQuery(
                    string.Format(
                     "{0} {1} @country {2} (@featurecode PPPL | PPPLA)",
                     cityName,
                     stateName,
                     countryName
                     ),
                    "geoname"
                );
            //}
            //sphinxClient.AddQuery("san diego CA @Country US @FeatureCode PPPL | PPPLA");

            var result = sphinxClient.RunQueries();
        }

        static void SearchFacilityNearby()
        {
            // San Diego
            //var lat = 32.82;
            //var lng = -117.13;

            // Phoenix
            var lat = 33.43;
            var lng = -112.02;

            var sphinxClient = new SphinxClient("192.168.1.210", 9312);
            sphinxClient.Limit = 100;
            sphinxClient.MatchMode = MatchEnum.Extended2;
            //sphinxClient.Sort = SortEnum.Expression;

            // 20 miles = 32,186.88 meters
            sphinxClient.SetFilterFloatRange("@geodist", 0, 100000, false);

            // Set our anchor (centroid)
            sphinxClient.GeoAnchor = new AnchorPoint()
            {
                AttributeLatitude = "latitude",
                AttributeLongitude = "longitude",
                LatitudeDegrees = lat,
                LongitudeDegrees = lng
            };

            var result = sphinxClient.Query("", "facility");
        }

        static void SearchTest()
        {
            string keyWord = "phoe";

            var sphinxClient = new SphinxClient("192.168.1.210", 9312);
            sphinxClient.Limit = 10;
            sphinxClient.MatchMode = MatchEnum.Extended2;
            sphinxClient.Sort = SortEnum.Expression;
            sphinxClient.SortBy = string.Format(
                "@weight + IF(({0} / namelength) * 100=100, population, ({0} / namelength) * 100)",
                keyWord.Length.ToString()
                );

            //sphinxClient.FieldWeights.Add("population", 2);

            sphinxClient.SetFilterRange("facilityinrange", 1, 5000, false);

            if (!keyWord.Contains(' '))
                keyWord += "*";

            // Build the query
            var query = string.Format(
                 "{0} (@featurecode PPPL | PPPLA)",
                 keyWord
                 );

            var sphinxResult = sphinxClient.Query(query, "geoname");
        }

        //static double ToRadians(this double degrees)
        //{
        //    return degrees * (Math.PI / 180);
        //}

        //static double ToDegrees(this double radians)
        //{
        //    return radians * (180 / Math.PI);
        //}
    }
}
